import request from '@/router/axios';
import Mock from 'mockjs';

//ids
// var ids = Mock.mock({"id|1-50":1000});
//获取时间

function getCurrentDate(){
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth();//得到月份
    var date = now.getDate();//得到日期
    var day = now.getDay();//得到周几
    var hour = now.getHours();//得到小时
    var minu = now.getMinutes();//得到分钟
    var sec = now.getSeconds();//得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
    return time;
}

let _list = [
    {
        addTime: "2021-03-27 22:04:46", //开始时间  
        discount: 1, //优惠金额
        discountMember: 4, //团购要求人数
        expireTime: "2029-03-27 22:04:35", //结束时间
        goodsId: 136, //商品id
        goodsName: "实用logo晴雨伞防晒防紫外线银胶伞活动广告雨伞",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2020/751/018/16934810157_832093914.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:02:36",
        discount: 3,
        discountMember: 10,
        expireTime: "2028-03-27 22:02:18",
        goodsId: 137,
        goodsName: "iphone12全降解挂绳手机壳彩绘个性定制苹果12斜跨保护壳厂家适用",
        picUrl: "https://cbu01.alicdn.com/img/ibank/O1CN01bbEBCL1W7sfE5BDHA_!!941952742-0-cib.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:01:21",
        discount: 3,
        discountMember: 9,
        expireTime: "2026-03-14 00:00:00",
        goodsId: 138,
        goodsName: "新款速递白色TPE+TPU黑圈撞色手机壳适用苹果 iPhone 11定制图案",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2019/763/372/12823273367_1497136090.400x400.jpg",
    },
    {
        addTime: "2021-03-27 21:56:33",
        discount: 3,
        discountMember: 10,
        expireTime: "2029-03-26 00:00:00",
        goodsId: 139,
        goodsName: "适用苹果表带Iwatch 1/2/3/4/5/6代 apple 双色双扣撞色表带直销",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2020/994/239/23170932499_1497136090.400x400.jpg",
    },
    {
        addTime: "2021-03-06 22:36:47",
        discount: 3,
        discountMember: 2,
        expireTime: "2021-03-31 16:44:46",
        goodsId: 140,
        goodsName: "1.2m泰迪小熊毛绒玩具熊公仔布娃娃网店免费代理生日礼物厂家",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2018/535/758/9589857535_547963073.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:04:46", //开始时间  
        discount: 1, //优惠金额
        discountMember: 4, //团购要求人数
        expireTime: "2029-03-27 22:04:35", //结束时间
        goodsId: 141, //商品id
        goodsName: "实用logo晴雨伞防晒防紫外线银胶伞活动广告雨伞",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2020/751/018/16934810157_832093914.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:02:36",
        discount: 3,
        discountMember: 10,
        expireTime: "2028-03-27 22:02:18",
        goodsId: 142,
        goodsName: "iphone12全降解挂绳手机壳彩绘个性定制苹果12斜跨保护壳厂家适用",
        picUrl: "https://cbu01.alicdn.com/img/ibank/O1CN01bbEBCL1W7sfE5BDHA_!!941952742-0-cib.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:01:21",
        discount: 3,
        discountMember: 9,
        expireTime: "2026-03-14 00:00:00",
        goodsId: 143,
        goodsName: "新款速递白色TPE+TPU黑圈撞色手机壳适用苹果 iPhone 11定制图案",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2019/763/372/12823273367_1497136090.400x400.jpg",
    },
    {
        addTime: "2021-03-27 21:56:33",
        discount: 3,
        discountMember: 10,
        expireTime: "2029-03-26 00:00:00",
        goodsId: 144,
        goodsName: "适用苹果表带Iwatch 1/2/3/4/5/6代 apple 双色双扣撞色表带直销",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2020/994/239/23170932499_1497136090.400x400.jpg",
    },
    {
        addTime: "2021-03-06 22:36:47",
        discount: 3,
        discountMember: 2,
        expireTime: "2021-03-31 16:44:46",
        goodsId: 145,
        goodsName: "1.2m泰迪小熊毛绒玩具熊公仔布娃娃网店免费代理生日礼物厂家",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2018/535/758/9589857535_547963073.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:04:46", //开始时间  
        discount: 1, //优惠金额
        discountMember: 4, //团购要求人数
        expireTime: "2029-03-27 22:04:35", //结束时间
        goodsId: 146, //商品id
        goodsName: "实用logo晴雨伞防晒防紫外线银胶伞活动广告雨伞",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2020/751/018/16934810157_832093914.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:02:36",
        discount: 3,
        discountMember: 10,
        expireTime: "2028-03-27 22:02:18",
        goodsId: 147,
        goodsName: "iphone12全降解挂绳手机壳彩绘个性定制苹果12斜跨保护壳厂家适用",
        picUrl: "https://cbu01.alicdn.com/img/ibank/O1CN01bbEBCL1W7sfE5BDHA_!!941952742-0-cib.400x400.jpg",
    },
    {
        addTime: "2021-03-27 22:01:21",
        discount: 3,
        discountMember: 9,
        expireTime: "2026-03-14 00:00:00",
        goodsId: 148,
        goodsName: "新款速递白色TPE+TPU黑圈撞色手机壳适用苹果 iPhone 11定制图案",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2019/763/372/12823273367_1497136090.400x400.jpg",
    },
    {
        addTime: "2021-03-27 21:56:33",
        discount: 3,
        discountMember: 10,
        expireTime: "2029-03-26 00:00:00",
        goodsId: 149,
        goodsName: "适用苹果表带Iwatch 1/2/3/4/5/6代 apple 双色双扣撞色表带直销",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2020/994/239/23170932499_1497136090.400x400.jpg",
    },
    {
        addTime: "2021-03-06 22:36:47",
        discount: 3,
        discountMember: 2,
        expireTime: "2021-03-31 16:44:46",
        goodsId: 150,
        goodsName: "1.2m泰迪小熊毛绒玩具熊公仔布娃娃网店免费代理生日礼物厂家",
        picUrl: "https://cbu01.alicdn.com/img/ibank/2018/535/758/9589857535_547963073.400x400.jpg",
    }
]

// 获取数据列表
export const bulkList = (data) =>request({
    url:'/bulkList',
    method:'get',
    data
})
Mock.mock(/\/bulkList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

//添加团购
export const addBulk = (data) =>request({
    url:'/addBulk',
    mothed:'post',
    data
});
Mock.mock(/\/addBulk/,config=>{
    var ids = Mock.mock({"id|1-100":1000});
    let datas = JSON.parse(config.body)
    let {goodsName,expireTime,discount,discountMember} = datas;
    if(goodsName && expireTime && discount && discountMember){
        _list.unshift({
            goodsName: goodsName,
            expireTime: expireTime,
            goodsId: ids.id, 
            discount: discount,
            discountMember: discountMember,
            picUrl:datas.picUrl?datas.picUrl:'',
            addTime:getCurrentDate(),
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除团购
export const detBulk = (data) =>request({
    url:'/detBulk',
    mothed:'delete',
    data
})
Mock.mock(/\/detBulk/,config=>{
    let {goodsId} = JSON.parse(config.body);
    if(goodsId){
        for(let index in _list){
            if(_list[index].goodsId==goodsId){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询团购
export const queryBulk = (data) =>request({
    url:'/queryBulk',
    mothed:'get',
    data
})
Mock.mock(/\/queryBulk/,config=>{
    let {goodsId} = JSON.parse(config.body);
    let arr = [];
    if(goodsId){
        arr = _list.filter((item,index)=>item.goodsId==goodsId);
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改团购数据
export const editBulk = (data) =>request({
    url:'/editBulk',
    mothed:'put',
    data
})
Mock.mock(/\/editBulk/,config=>{
    let datas = JSON.parse(config.body);
    let {goodsId} = datas;
    if(goodsId){
        for(let item of _list){
            if(item.goodsId==goodsId){
                item.discount = datas.discount?datas.discount:item.discount;
                item.discountMember= datas.discountMember?datas.discountMember:item.discountMember;
                item.expireTime = datas.expireTime?datas.expireTime:item.expireTime
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})